package com.mushuilingfeng.ddd.user.domain.user.entity;

import com.mushuilingfeng.ddd.framework.factory.DomainRepositoryFactory;
import com.mushuilingfeng.ddd.user.domain.base.BaseEntity;
import com.mushuilingfeng.ddd.user.domain.user.repository.IUserLoginDeviceRepository;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

/**
 * @author 沐水淋风
 * @date 2024/10/5
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UserLoginDevice extends BaseEntity {
    private Long userId;
    private String deviceCode;
    private String deviceName;
    private Date firstLoginTime;
    private Date lastLoginTime;
    
    @Override
    public void saveNew() {
        super.saveNew();
        this.firstLoginTime = new Date();
        this.lastLoginTime = this.firstLoginTime;
        DomainRepositoryFactory.getRepository(IUserLoginDeviceRepository.class).save(this);
    }
    
    public void updateLastLoginTime() {
        this.lastLoginTime = new Date();
        DomainRepositoryFactory.getRepository(IUserLoginDeviceRepository.class).updateLastLoginTime(id, this.lastLoginTime);
    }
    
}
